// pages/mall/mall.js

import MallService from "../../class/service/MallService";
import Router from "../../class/Router";
import Tips from "../../utils/Tips";
var WxParse = require('../../wxParse/wxParse.js');

const mallService = new MallService();

Page({

  /**
   * 页面的初始数据
   */
  data: {

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;

    Tips.loading();

    mallService.list().then(data => {
      
      Tips.loaded();
      
      that.setData({
        "init": true,
        "skus": data.skus
      })

    }, res => {
      Tips.error("获取商品详情失败");
      that.setData({
        "init": true
      })
    })
  },

  splitTags: function() {
    var that = this;
    var skus = that.data.skus;
    
    for(var i in skus) {
      var tagJsons = [];
      var sku = skus[i];
      var tags = sku.tags.split('|');
      for(var j in tags) {
        var tagJson = {}
        tagJson["class"] = tags[j].split(',')[0];
        tagJson["text"] = tags[j].split(',')[1];
        tagJsons.push(tagJson)
      }
      skus[i]['tagJsons'] = tagJsons
    }

    console.log(skus)

    that.setData({
      skus: skus
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  }
})